<?php

namespace app\model\v1\recovery;


use constants\ProductConstants;
use crmeb\traits\ModelTrait;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Db;
use think\Model;

class RecoveryReport extends Model
{
    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];

    protected $table = 'eb_recovery_report';



    public function cate ()
    {
        return $this->hasOne ('recovery_question_cate','id','cate');
    }


    /**
     * 举报管理列表
     * @param $param
     * @return array
     */
    public static function getReportList($param = [])
    {
        $order = ["a.id"=>"desc"];

        $data['data'] = self::alias("a")
            ->field ("a.*,FROM_UNIXTIME(a.create_time) as create_time,b.cate_name,c.nickname,c.phone,
             case when a.module=1 then '闲置转让' 
                when a.module=2 then '求职招聘' 
                end as module_text 
            ")
            ->join ('recovery_question_cate b','a.cate=b.id','left')
            ->join ('user c','a.uid=c.uid','left')
            ->where(function ($query) use ($param){
                $query->whereNull("a.delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('a.create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                //根据用户昵称/联系方式搜索
                if(!empty($param["word"])) $query->where("c.nickname",'like', '%'.$param["word"].'%')->whereOr("c.phone",'like', '%'.$param["word"].'%');
            })
            ->order($order)
            ->page($param['page'], $param['limit'])
            ->select()->toArray();

        $data['count'] = self::alias("a")
            ->field ("a.*,b.cate_name,c.nickname,c.phone")
            ->join ('recovery_question_cate b','a.cate=b.id','left')
            ->join ('user c','a.uid=c.uid','left')
            ->where(function ($query) use ($param){
                $query->whereNull("a.delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('a.create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("c.nickname",'like', '%'.$param["word"].'%')->whereOr("c.phone",'like', '%'.$param["word"].'%');
            })
            ->count();

        return $data;
    }


    /**
     * 举报管理列表 - 查看
     * @param $param
     * @return mixed
     */
    public static function getReportDetail($param = [])
    {
        $data = self::alias("a")
            ->field ("a.*,b.cate_name,c.nickname,c.phone")
            ->join ('recovery_question_cate b','a.cate=b.id','left')
            ->join ('user c','a.uid=c.uid','left')
            ->where(function ($query) use ($param){
                $query->whereNull("a.delete_time");
                $query->where("a.id", $param["id"]);
            })
            ->find();

        return $data;
    }


}